#include "ServerListenView.h"

ServerListenView::ServerListenView(Screen *s, Model *m, LPDIRECT3DDEVICE9 dd3d): View(s, m, dd3d)
{
  _model = (ServerListenModel*)m;
  D3DXCreateFont(_dd3dDevice, 60, 0, FW_NORMAL, 1, false, DEFAULT_CHARSET,
                 OUT_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH || FF_DONTCARE,
                 "Arial", &_font);
  D3DXCreateFont(_dd3dDevice, 40, 0, FW_NORMAL, 1, false, DEFAULT_CHARSET,
                 OUT_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH || FF_DONTCARE,
                 "Arial", &_fontOption);
  InitRects();
}

void ServerListenView::InitRects(void)
{
  SetRect(&_titleRect, 100, 100, 1000, 200);
  SetRect(&_textRect[0], 400, 500, 700, 600);
  SetRect(&_textRect[1], 400, 600, 700, 700);
}

ServerListenView::~ServerListenView(void)
{
  _font->Release();
  _fontOption->Release();
}

void ServerListenView::Draw(void)
{
  HRESULT res;
  if(background != NULL)
  {
    GlobalDebug::GetInstance()->AssertAndLogResult(_sprite->Draw(this->background, NULL, NULL, NULL, D3DCOLOR_XRGB(255,255,255)), "NetworkMenuView::Draw background");
    _sprite->End();
    _sprite->Begin(D3DXSPRITE_ALPHABLEND);
  }

  res = _font->DrawTextA(NULL, _serverListenTitle, _serverListenTitleLen, 
                         &_titleRect,  DT_CENTER | DT_VCENTER, _serverListenTextColor);
  int item = _model->GetSelectedMenuItem();
  res = _fontOption->DrawText(NULL, _serverStartOption, _serverStartLen, &_textRect[0],
                        DT_CENTER | DT_VCENTER, item == 0 ? _serverListenTextColor : _serverListenSelColor);
  res = _fontOption->DrawText(NULL, _serverBackOption, _serverBackLen, &_textRect[1],
                        DT_CENTER | DT_VCENTER, item == 1 ? _serverListenTextColor : _serverListenSelColor);

  RECT tempRect;
  SetRect(&tempRect, 400, 300, 700, 400);
  if(_model->mListenAddr != NULL)
    res = _fontOption->DrawText(NULL, _model->mListenAddr, strlen(_model->mListenAddr), &tempRect, DT_CENTER | DT_VCENTER, _serverListenTextColor);
  char drawBuffer[NUMBER_LENGTH] = {0};
  SecureZeroMemory(drawBuffer, NUMBER_LENGTH);
  sprintf(drawBuffer, "%hu", _model->mListenPort);
  SetRect(&tempRect, 400, 400, 700, 500);
  res = _fontOption->DrawText(NULL, drawBuffer, strlen(drawBuffer), &tempRect, DT_CENTER | DT_VCENTER, _serverListenTextColor);
}

void ServerListenView::HandleMessage(Message* msg)
{
  delete msg;
}

void ServerListenView::PreRender(void)
{
  GlobalDebug::GetInstance()->AssertAndLogResult(_sprite->Begin(D3DXSPRITE_ALPHABLEND), "View::PreRender Sprite::Begin");
}

void ServerListenView::PostRender(void)
{
  GlobalDebug::GetInstance()->AssertAndLogResult(_sprite->End(), "View::PostRender Sprite::End");
}
